//
//  ComplaintDetailVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/19.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "ComplaintDetailVC.h"
#import "ComplainsRecordsModel.h"
#import "XLImageViewer.h"

@interface ComplaintDetailVC ()<UITableViewDelegate,UITableViewDataSource>{
    UIView* _imgParent;
    UIView* _imgParentTopView;
    FlexFrameView *header;
////    int index;  // 记录图片的位置
//    int indexReturn;  // 记录图片的位置
//    int  indexReplace; //替换的位置
}
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *arrNetImgs;

@property (nonatomic,strong) CJLabel *labComplainer;
@property (nonatomic,strong) CJLabel *labComplainerTitle;
@property (nonatomic,strong) CJLabel *labAddress;
@property (nonatomic,strong) CJLabel *labTime;
@property (nonatomic,strong) CJLabel *labOrderNum;
@property (nonatomic,strong) CJLabel *labComplaintReson;
@property (nonatomic,strong) CJLabel *labProcessState; // 处理状态
@property (nonatomic,strong) CJLabel *labResult;
@property (nonatomic,strong) ComplainsRecordsModel *modelComplainsRecord;
//满意度：1满意，2不满意
@property (nonatomic,copy) NSString *strSatisfy;
// 满意度
@property (nonatomic,strong) UIView *viewSatisfy;
@property (nonatomic,strong) CJLabel *labSatisfy; //
@property (nonatomic,strong) UIView *viewSatisfyTouch; //
//处理结果展示
@property (nonatomic,strong) UIView *viewDisposeResultShow;
@property (nonatomic,strong) CJLabel *labDisposeResultShow; //
//处理结果反馈
@property (nonatomic,strong) UIView *viewDisposeResult; // 处理结果
@property (nonatomic,strong) FlexTextView *textViewDisposeResult;
@end

@implementation ComplaintDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"投诉详情")];
    [self setBackLeftButton:@""];
    [self.view addSubview:self.tableView];
//    [self createTableView];
    [self loadData];
}

- (void)loadData{

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/orderDetailAbnormal/%@",CJBaseUrl,self.strCode);
        
    NSDictionary *dicPra = nil;
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
        
        NSError *error ;
        ws.modelComplainsRecord = [[ComplainsRecordsModel alloc]initWithDictionary:responseObject error:&error];
        [ws createTableView];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];
  
}

/// 创建tableview 等
- (void)createTableView{
    
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, 0, kScreenWidth, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"ComplaintDetailHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    
    if ([self.strType isEqualToString:@"0"]) {
        self.labComplainer.text = self.modelComplainsRecord.driverName;
        self.labComplainerTitle.text = @"被投诉人";
     }else{
         self.labComplainer.text = self.modelComplainsRecord.driverName;
         self.labComplainerTitle.text = @"投诉人";
     }
    
    self.labAddress.text = self.modelComplainsRecord.address;
    self.labTime.text = self.modelComplainsRecord.occurrenceTime;
    self.labOrderNum.text = self.modelComplainsRecord.mainOrderNumber ;
    self.labComplaintReson.text = CJStringWithFormat(@"%@:%@",self.modelComplainsRecord.abnormalTypeName ,self.modelComplainsRecord.abnormalRemark);
    self.labDisposeResultShow.text = self.modelComplainsRecord.abnormalFeedback;

    [self.arrNetImgs removeAllObjects];
    
    if (self.modelComplainsRecord.abnormalImageList.count == 0) {
        _imgParent.hidden = YES;
        _imgParentTopView.hidden = YES;
    }else{
        _imgParent.hidden = NO;
        _imgParentTopView.hidden = NO;

        [self.arrNetImgs addObjectsFromArray:self.modelComplainsRecord.abnormalImageList];

        for (int i = 0; i < self.arrNetImgs.count; i++) {

            UIImageView* cell = [[UIImageView alloc]init];
            cell.userInteractionEnabled = YES;
            //        [cell enableFlexLayout:YES];
             UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickToSeeBigCell:)];
            cell.tag = i + 1000;
            [cell enableFlexLayout:YES];
            [cell addGestureRecognizer:tap];

            [cell setLayoutAttrStrings:@[
                                         @"width",@"75",
                                         @"height",@"75",
                                         @"margin",@"1%",
                                         @"alignItems",@"center",
                                         @"justifyContent",@"center",
                                         ]];
            [cell setViewAttr:@"bgColor" Value:@"#E4E9ED"];
            [cell setViewAttr:@"borderRadius" Value:@"0"];
            [_imgParent insertSubview:cell atIndex:i];
            [_imgParent markDirty];
            //    cell.layer.contents = (id) img.CGImage;
            //    UIImageView *imgV;
            [cell sd_setImageWithURL:[NSURL URLWithString:self.arrNetImgs[i]] placeholderImage:[UIImage imageNamed:@"zcj_error_image"]];
        }
    }
    
    if (self.modelComplainsRecord.isHandle == 1) {
        self.labProcessState.text = @"已处理";
    }else{
        self.labProcessState.text = @"处理中";
    }

    self.labResult.text = self.modelComplainsRecord.handleIdea;
    
    // self.modelComplainsRecord.satisfy == 0
    // 满意度
    //投诉来源：0->投诉司机 1->投诉货
    if ([self.strType isEqualToString:@"1"]) {
        self.viewSatisfyTouch.hidden = YES;
        self.viewDisposeResult.hidden = YES;
     }else{
        
        if (self.modelComplainsRecord.isHandle == 1  && self.modelComplainsRecord.satisfy == 0) {
            
            self.viewSatisfyTouch.hidden = NO;
            //         self.viewSatisfy.hidden = YES;
            //        self.viewSatisfy.hidden = YES;
            self.viewDisposeResult.hidden = NO;
         }else{
             self.viewSatisfyTouch.hidden = YES;
             self.viewDisposeResult.hidden = YES;
         }
     }
    
    if (self.modelComplainsRecord.satisfy == 1) {
        self.labSatisfy.text = @"满意";
        self.viewSatisfy.hidden = NO;
        self.viewDisposeResultShow.hidden = NO;
     }else if (self.modelComplainsRecord.satisfy == 2){
         self.labSatisfy.text = @"不满意";
         self.viewSatisfy.hidden = NO;
         self.viewDisposeResultShow.hidden = NO;
     }else{
         self.viewSatisfy.hidden = YES;
         self.viewDisposeResultShow.hidden = YES;
     }
    
    [header layoutIfNeeded];

    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}

-(void)clickToSeeBigCell:(UIGestureRecognizer*)sender{
    UIView* cell = sender.view;
    [[XLImageViewer shareInstanse] showNetImages:self.arrNetImgs index:(int)cell.tag - 1000 fromImageContainer:cell];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MeViewCellId = @"DriverAouthVCcellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MeViewCellId];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MeViewCellId];
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

/// 满意
- (void)btnTouchApprovingAction{
    self.strSatisfy = @"1";
    [self getApprovingInfo];
}

/// 不满意
- (void)btnTouchDisApprovingAction{
    self.strSatisfy = @"2";
    [self getApprovingInfo];
}

- (void)getApprovingInfo{
    
    if (self.textViewDisposeResult.text.length > 255) {
        [CJAlert showStringDismissWithDelay:@"处理结果反馈内容过长"];
        return;
    }
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/orderDetailAbnormal/satisfyConfirm",CJBaseUrl);
    NSDictionary *dicPar = @{@"code":self.strCode,
                             @"satisfy":self.strSatisfy,
                             @"abnormalFeedback":[CJControl getThreeUnaryValueStr:self.textViewDisposeResult.text]
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] POST:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        [ws pop];
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

#pragma mark ----------lazy
- (NSMutableArray *)arrNetImgs{
    if (!_arrNetImgs) {
        _arrNetImgs = [[NSMutableArray alloc]init];
    }
    return _arrNetImgs;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        ;
    }
    return _tableView;
}

@end

